//
//  WLMVisitorLoginView.swift
//  WeiBo
//
//  Created by Eric on 2017-01-06.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit
import SnapKit

//代理协议声明
@objc protocol WLMVisitorLoginViewDelegate: NSObjectProtocol {
    //可选方法
    @objc optional func userWillLogin()
    //方法名
    func userWillRegister()
}


class WLMVisitorLoginView: UIView {
    
    //声明属性,懒加载
    //大房子
    lazy var house: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //圆圈
    lazy var circleImage: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //提示文字
    lazy var tip : UILabel = {
        let label = UILabel(text: "关注一些人,回到这里看看有什么惊喜关注一些人,回到这里看看有什么惊喜", fonSize: 14, textColor: UIColor.flatGrayDark)
        label.preferredMaxLayoutWidth = 220
        label.textAlignment = .center
        return label
    }()
    
    //登陆按钮
    lazy var loginBtn : UIButton = {
    
        let btn = UIButton(backgroundImageName: "common_button_white", title: "登录", fontSize: 14, textColor: UIColor.flatOrange)
        return btn
    }()
    
    //注册按钮
    lazy var registerBtn : UIButton = {
    let btn = UIButton(backgroundImageName: "common_button_white", title: "注册", fontSize: 14, textColor: UIColor.flatGrayDark)
        
        return btn
    }()
    
    //遮罩
    lazy var maskImage : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))

    //代理属性声明,要用弱引用
    weak var delegate: WLMVisitorLoginViewDelegate?

    
    //自定义访客视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //界面搭建
    private func setupUI() {
        //添加子视图
        addSubview(circleImage)
        addSubview(maskImage)
        addSubview(tip)
        addSubview(house)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置约束
        circleImage.snp.makeConstraints { (make) in
            make.centerY.equalTo(self).offset(-80)
            make.centerX.equalTo(self)
        }
        
        house.snp.makeConstraints { (make) in
            make.center.equalTo(circleImage)
        }
        
        tip.snp.makeConstraints { (make) in
            make.top.equalTo(circleImage.snp.bottom).offset(10)
            make.centerX.equalTo(self)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tip)
            make.top.equalTo(tip.snp.bottom).offset(10)
            make.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tip)
            make.top.equalTo(tip.snp.bottom).offset(10)
            make.width.equalTo(100)
        }
        
        maskImage.snp.makeConstraints { (make) in
            make.center.equalTo(circleImage)
        }
        
        //设置背景色
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //按钮点击事件
        loginBtn.addTarget(self, action: #selector(clickLogin), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(clickRegister), for: .touchUpOutside)
    }
    
    func clickLogin() {
        delegate?.userWillLogin?()
    }
    
    func clickRegister() {
        delegate?.userWillRegister()
    }
    
    
    //该方法设置访客视图,并可以赋默认值
    func visitorViewInfo(name: String, title: String, isHome: Bool = false) {
        
        //修改图片,以及文字
        self.tip.text = title
        self.circleImage.image = UIImage(named: name)
        if isHome {
            //开始动画
            startAnimation()
        }
        else {
            //隐藏房子,隐藏遮罩
            house.isHidden = true
            maskImage.isHidden = true
        }
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.toValue = M_PI * 2
        
        //动画不被移除,默认true
        animation.isRemovedOnCompletion = false
        //添加到图层
        circleImage.layer.add(animation, forKey: nil)
    }
    
    

}
